// 对字符串进行编码
function encodeData(str) {  
	return encodeURIComponent(str).replace(/\%/g, "%25").replace(/\-/g, "%2D").replace(/\_/g, "%5F").replace(/\./g, "%2E").replace(/\!/g, "%21").replace(/\~/g, "%7E").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29");
}  
//对字符串进行解码
function decodeData(str){
    try{
        return decodeURIComponent(str.replace(/\%25/g, "%").replace(/\%2D/g, "-").replace(/\%5F/g, "_").replace(/\%2E/g, ".").replace(/\%21/g, "!").replace(/\%7E/g, "~").replace(/\%2A/g, "*").replace(/\%27/g, "'").replace(/\%28/g, "(").replace(/\%29/g, ")"));
    }catch (e) {
    }
    return "";
}
function fmoney(s, n) {
	if(s == 0) {
		return 0;
	}
    if (s == "" || s == undefined || isNaN(s)) {
        return "";
    }
    if(!isNumber(s)) {
        return "";
    }
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + ""; 
	var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
	t = ""; 
	for (var i = 0; i < l.length; i++) { 
		t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); 
	} 
	return t.split("").reverse().join("") + "." + r;
}

function isNumber(number)
{
   // 正整数
   var reg1 = /^[0-9]*[1-9][0-9]*$/;
   // 正浮点数
   var reg2 = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;
   if(reg1.test(number) == false && reg2.test(number) == false)
   {
       return  false;  
   }
   return true;
}

function isNumberZero(number)
{
   // 非负整数（正整数 + 0）
   var reg1 = /^\d+$/;
   //非负浮点数（正浮点数 + 0）
   var reg2 = /^\d+(\.\d+)?$/;
   if(reg1.test(number) == false && reg2.test(number) == false)
   {
       return  false;  
   }
   return true;
}

// 加载特效开始
function loadStart(imgUrl) {  
	var _html = "<div class='overlay'></div><div id='AjaxLoading' class='showbox'>"
				+"<div class='loadingWord'><img src='"+imgUrl+"/images/loading.gif'>处理中，请稍候...</div>"
				+"</div>   ";  
	$("body").append(_html);  			
	var h = $(document).height();
	var showboxtop= (document.documentElement.scrollTop + (document.documentElement.clientHeight - $(".showbox").height()) / 2);
	$(".overlay").css({"height": h });	
	$(".overlay").css({'display':'block','opacity':'0.6'});	
	$(".showbox").stop(true).animate({'margin-top':showboxtop+'px','opacity':'1'},20);		
}  
// 加载特效结束
function loadEnd() {  
	$(".showbox").stop(true).animate({'opacity':'0'},300);			
	$(".overlay").css({'display':'none','opacity':'0'});
	$(".overlay").remove();
}

function fileSelect(obj) {
	$(obj).parent().children("input[name='file']").click();
}

function detailfileSelect(obj) {
	$(obj).parent().children("input[name='detailfile']").click();
}

function fileSelected(obj) {
	getPath($(obj).parent().children('div.thumbnail').children(),obj,$(obj).val());
}

function removeFile(obj){
	if ($("button[name='imgDel']").length == 1){
		Modal.alert({msg:'至少选择一张图片！',title: '警告',btnok: '确定',btncl:'取消'});
		return;
	}
	$(obj).parents(".form-group").remove();
}

function removeDetail(obj){

	$(obj).parents(".detail").remove();
}

var getPath=function(obj,fileQuery,transImg){
	if(window.navigator.userAgent.indexOf("MSIE")>=1){ 
		obj.attr("src", transImg);
	}
	else{
		var file =fileQuery.files[0];  
		var reader = new FileReader();  
		reader.onload = function(e){
			obj.attr("src", this.result);
		};
		reader.readAsDataURL(file);  
	}
};

$(function () {
	var divMessage = "<div id='ycf-alert' class='modal' aria-hidden='true'>";
	divMessage += "<div class='modal-dialog modal-sm'>";
	divMessage += "<div class='modal-content'>";
	divMessage += "<div class='modal-header'>";
	divMessage += "<button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>";
	divMessage += "<h5 class='modal-title'><i class='fa fa-exclamation-circle'></i> [Title]</h5>";
	divMessage += "</div>";
	divMessage += "<div class='modal-body small'>";
	divMessage += "<p>[Message]</p>";
	divMessage += "</div>";
	divMessage += "<div class='modal-footer' >";
	divMessage += "<button type='button' class='btn btn-primary ok' data-dismiss='modal'>[BtnOk]</button>";
	divMessage += "<button type='button' class='btn btn-default cancel' data-dismiss='modal'>[BtnCancel]</button>";
	divMessage += "</div></div></div></div>";
	
	
	$("#popMessage").append(divMessage);
	window.Modal = function () {
	    var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');
	    var alr = $("#ycf-alert");
	    var ahtml = alr.html();

	    var _alert = function (options) {
	      alr.html(ahtml);	// 复原
	      alr.find('.ok').removeClass('btn-success').addClass('btn-primary');
	      alr.find('.cancel').hide();
	      _dialog(options);

	      return {
	        on: function (callback) {
	          if (callback && callback instanceof Function) {
	            alr.find('.ok').click(function () { callback(true); });
	          }
	        }
	      };
	    };

	    var _confirm = function (options) {
	      alr.html(ahtml); // 复原
	      alr.find('.ok').removeClass('btn-primary').addClass('btn-success');
	      alr.find('.cancel').show();
	      _dialog(options);

	      return {
	        on: function (callback) {
	          if (callback && callback instanceof Function) {
	            alr.find('.ok').click(function () { callback(true); });
	            alr.find('.cancel').click(function () { callback(false); });
	          }
	        }
	      };
	    };

	    var _dialog = function (options) {
	      var ops = {
	        msg: "提示内容",
	        title: "操作提示",
	        btnok: "确定",
	        btncl: "取消"
	      };

	      $.extend(ops, options);

	      console.log(alr);

	      var html = alr.html().replace(reg, function (node, key) {
	        return {
	          Title: ops.title,
	          Message: ops.msg,
	          BtnOk: ops.btnok,
	          BtnCancel: ops.btncl
	        }[key];
	      });
	      
	      alr.html(html);
	      alr.modal({
	        width: 500,
	        backdrop: 'static'
	      });
	    };

	    return {
	      alert: _alert,
	      confirm: _confirm
	    };

	  }();
});